import React, { Component } from 'react';
import FileList from './components/FileList/FileList';
import { Loading } from '@alifd/next';
import request from '../../instances/request';
import './index.scss';
import { minHeight } from "../../conf/config";

//todo 应当知道user-type，这样可以决定用户能否修改平台公告
export default class FileShare extends Component {
  static displayName = 'FileShare';

  constructor(props) {
    super(props);
    this.state = {
      __loading: true,
      showEditButton: false,
      UserId: -1,
    };
  }

  async getAndSetUserPrivilege() {
    let res = await request.get('/personalData');
    let { code, msg } = res.data;
    if( code !== 0 ) {
      throw new Error(`code为${code}`);
    }
    let { user } = msg;
    let showEditButton = user.browsePrivileges.fileShareEdit;
    this.setState({
      UserId: user.id,
      showEditButton,
      __loading: false,
    });
  }

  async componentDidMount() {
    await this.getAndSetUserPrivilege();
  }

  render() {
    return (
      this.state.__loading ?
        <Loading tip="加载中" size="large" fullScreen/> :
        <div className="fileShare" style={{ minHeight: `${minHeight}px` }}>
          <FileList
            UserId={this.state.UserId}
            showEditButton={this.state.showEditButton}
            limit={8}
          />
        </div>
    );
  }
}
